;;; http://mathworld.wolfram.com/AmicablePair.html
(defconstant *first-few-pairs* '((220 284) (1184 1210) (2620 2924) (5020 5564) (6232 6368) (10744 10856) (12285 14595) (17296 18416) (63020 76084)))

(defun flatten-list (alist)
  (reduce
    (lambda (x y) (append x y))
    alist))

(defun p19 ()
  (reduce
    '+
    (remove-if 
      (lambda (n) (> n 10000))
      (flatten-list *first-few-pairs*))))

(format t "~a~%" (time (p19)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun list-divisors (n)
  (loop for i from 1 to (sqrt n)
        when (= (mod n i) 0)
        collect i
        and collect (/ n i)))

(defun sum-of-restricted-divisors (n)
  (- (reduce #'+ (list-divisors n)) n))

(defun sum-of-amicable-pairs (limit)
  (loop for n from 1 to limit
        for m = (sum-of-restricted-divisors n)
        when (and (not (= m n))
                  (= (sum-of-restricted-divisors m) n))
             sum n))

;(format t "~a~%" (time (sum-of-amicable-pairs 10000)))
(format t "~a~%" (list-divisors 1800))
(format t "~a~%" (sum-of-restricted-divisors 1800))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; http://planetmath.org/encyclopedia/FormulaForSumOfDivisors.html
;;; use this formula to calculate sum of divisors will be more efficient.
;;; at Forum Thread 21, Page 2
